package com.czb.server.service.Impl;

import com.czb.server.pojo.Department;
import com.czb.server.mapper.DepartmentMapper;
import com.czb.server.pojo.RespBean;
import com.czb.server.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhanglishen
 * @since 2022-02-20
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> getAllDepartment() {
        return departmentMapper.getAllDepartments(-1);
    }

    @Override
    public RespBean putDepartment(Department department) {
        department.setEnabled(true);
        departmentMapper.putDepartment(department);
        return 1==department.getResult()?RespBean.success("添加成功",department):RespBean.error("添加失败");
    }

    /**
     * 删除部门
     * @param id
     * @return
     */
    @Override
    public RespBean deleteDep(Integer id) {
        Department dep=new Department();
        dep.setId(id);
        departmentMapper.deleteDep(dep);
        if (-2==dep.getResult())
            return RespBean.error("删除失败！请先删除该部门下的部门");
        else if (-1==dep.getResult())
            return RespBean.error("删除失败！该部门下还有员工，请先删除员工");
        else if (1==dep.getResult())
            return RespBean.success("删除成功");
        return RespBean.error("删除失败");
    }
}
